#ifndef __RCO_WORKTHREAD_H__
#define __RCO_WORKTHREAD_H__

#include <QMutex>
#include <QThread>
#include <QSemaphore>
#include <QByteArray>
#include <QEvent>
#include "cmdhandler.h"
#include "rcodef/rcointerface.h"

namespace rco
{

class RcoIODevice;

class RcoWorkThreadData
	: public QEvent
{
public:
	enum {RcoData = QEvent::User + 0x100};

	RcoWorkThreadData();

	RcoCommandHead rcoCmdHread;
	RcoBuffer	   bytes;
	int			   iTaskId;
};

class RcoWorkThread
	: public QThread
{
	Q_OBJECT
public:
	RcoWorkThread();
	~RcoWorkThread();

	static void* operator new (quint32 size);

	bool IsWorking() const;
	bool SetIODevice(RcoIODevice* pIODevice);
	RcoIODevice* GetIODevice();
	const RcoIODevice* GetIODevice() const;

	bool Execute(const RcoCommandHead& rcoCmdHead, const RcoBuffer& bytes, int iTaskId);

private slots:
	void Suidside();

signals:
	void TaskCompleted(RcoWorkThread* thread, RcoCommandHead* rcoCmdHead, RcoBuffer* bytes);

protected:
	virtual void run();
	void customEvent(QEvent * event);

	CommandsHander  m_cmdHandler;
	RcoCommandHead	m_rcoCmdHead;
	RcoBuffer		m_buf;
	int				m_iTaskId;
	RcoIODevice*	m_pIODevice;
};

};

#endif // __RCO_WORKTHREAD_H__